package com.pioneer.common.Task;

import com.pioneer.common.config.CommonConfig;
import com.pioneer.web.system.domain.SysDatabase;
import com.pioneer.web.system.service.ISysDatabaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * @author :wf
 * @description : 定时任务类
 * @create :2023-3-28 15:22:00
 */
@Slf4j
@Configuration
@EnableScheduling
public class TimeTask {

    @Resource
    private ISysDatabaseService sysDatabaseService;

    //每月1号2点执行一次
//    @Scheduled(cron = "0 0 2 1 * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void monthBackUpDataBase() {
        log.info("==================定时任务备份数据库开始执行，执行时间为：{}", new Date());
        Map<String, String> map = sysDatabaseService.delUrl(CommonConfig.getUrl());
        String fileName = sysDatabaseService.backupDatabase();
        // 插入数据库备份记录
        SysDatabase sysDatabase = new SysDatabase();
        sysDatabase.setName(map.get("database"));
        sysDatabase.setCreateBy("自动备份");
        sysDatabase.setCreateTime(LocalDateTime.now());
        sysDatabase.setUrl(fileName);
        sysDatabaseService.add(sysDatabase);
        log.info("==================定时任务备份数据库执行结束");
    }

}
